package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * <新闻 - 华声在线> 解析类
 *
 * @author KK
 *
 */
public class VocNewsAnalyse implements AnalyseNews {



	
	public boolean isDetailPage(String url) {
		String[] regex={
				"http://[a-z0-9]*.voc.com.cn/article/[0-9]{6}/[0-9]*.html",
				"http://jjw.voc.com.cn/view.php\\?tid-[0-9]*-cid-[0-9]*.html",
				"http://shpd.voc.com.cn/www/[0-9]*/[0-9]{4}-[0-9]{2}/[0-9]*.html",

				};

		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		String commnetNum=null;
		String clickNum=null;
		Document doc=Jsoup.parse(htmltxt);

		title=doc.select("div.contents h1").text();
		dateStr=doc.select("div.contents div.resource").text();
		content=HtmlCleaner.getContentHtml(url,doc.select("div#content"));
		commnetNum=doc.select("span#zaixiannum").text();

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.d10_left_x_1 div.contenttitle").text();
			dateStr=doc.select("div.d10_left_x_1 div.txtcenter").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.d10_left_x_1 div.contentarea"));
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.contents2 h1").text();
			dateStr=doc.select("div.contents2 div.resource2").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.contents2 div#content"));
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.leftbox > div.bt").text();
			dateStr=doc.select("div.leftbox > div.data").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.leftbox > div.nr"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.area > div.colleft > div.bigleft > div.bigtitle").text();
			dateStr=doc.select("div.area > div.colleft > div.bigleft > div.from").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.area > div.colleft > div.bigleft > div#zoom.news"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("title").text();
			title=StringUtils.substringBefore(title, " -");
			dateStr=StringUtils.substringAfterLast(url, "/");
			content=HtmlCleaner.getContentHtml(url,doc.select("div.main_tu"));
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div#l2").text();
			dateStr=doc.select("div#l3").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#l4"));
		}

		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.contentarea2"));
		}
		if (StringUtils.isBlank(content)) {
			content=doc.select("#content").html();
		}

		if(dateStr!=null){
			date=DateUtils.matchDate(dateStr);
		}
			NewsMeta news=new NewsMeta();
			news.setUrl(url);
			news.setType(1);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);


			String author=null;
			Elements select=doc.select("div.listcon div.auto a");
			if(select!=null && select.size()!=0){
				author=select.get(0).text();
			}
			if (StringUtils.isBlank(author)){
				author=JSoupUtils.matchAuthor(doc, "来源:");
			}
			news.setAuthor(author);
			return news;
	}



	public static void main(String[] args) {
		VocNewsAnalyse a=new VocNewsAnalyse();
		String url="http://cj.voc.com.cn/article/201405/201405060959457105001.html";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
